package core

import (
	"fmt"
	"gorm.io/driver/postgres"
	"gorm.io/gorm"
	"os"
	"ruankepaiming-server/model"
	"ruankepaiming-server/pkg/global"
	"strconv"
)

func GormP() *gorm.DB {
	pgCfg := global.CONFIG.Postgresql
	dsn := fmt.Sprintf("user=%s password=%s dbname=%s host=%s port=%d sslmode=disable TimeZone=Asia/Shanghai",
		pgCfg.User,
		pgCfg.Password,
		pgCfg.Name,
		pgCfg.Host,
		pgCfg.Port,
	)
	db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
	if err != nil {
		fmt.Println(err)
		os.Exit(0)
	}else{
		global.LOG.Info("postgresql "+pgCfg.Host +":" + strconv.Itoa(pgCfg.Port) +" connect successfully")
	}
	//db.AutoMigrate(&model.Asset{})
	//db.AutoMigrate(&model.Story{})
	//db.AutoMigrate(&model.Theme{})
	//db.AutoMigrate(&model.Partner{})
	//db.AutoMigrate(&model.User{})
	//db.AutoMigrate(&model.Config{})
	db.AutoMigrate(&model.Zongbang{})
	db.AutoMigrate(&model.Zhubang{})
	db.AutoMigrate(&model.Medical{})
	db.AutoMigrate(&model.Finance{})
	db.AutoMigrate(&model.Language{})
	db.AutoMigrate(&model.Law{})
	db.AutoMigrate(&model.Minzu{})
	db.AutoMigrate(&model.Tiyu{})
	db.AutoMigrate(&model.Hezuo{})
	db.AutoMigrate(&model.Minban{})
	db.AutoMigrate(&model.Yishu{})


	return db
}

